package com.weichen.studentgameexchange.member.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.weichen.studentgameexchange.member.dao.UserMapper;
import com.weichen.studentgameexchange.member.entity.User;
import com.weichen.studentgameexchange.common.exception.ServiceException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 功能：
 * 作者：惟尘
 * 日期：2025/1/29 11:46
 */
@Component
public class ReUserName {


    public void isRepeat(User user, UserMapper userMapper){
        LambdaQueryWrapper<User> dbuser = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<User> eq = dbuser.eq(user.getUsername() != null, User::getUsername, user.getUsername());
        List<User> list = userMapper.selectList(eq);
        if (!list.isEmpty()) {
            throw new ServiceException("用户名已存在！");
        }
    }

    public void isTogo(User user, UserMapper userMapper){
        int random = (int) (Math.random() * 10)+3;
        String userName = RandomUserName.randomName(false,random);
        if (user.getUsername()!=null)
            user.setUsername(null);
        user.setUsername(userName);
        LambdaQueryWrapper<User> dbuser = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<User> eq = dbuser.eq(user.getUsername() != null, User::getUsername, user.getUsername());
        List<User> list = userMapper.selectList(eq);
        if (!list.isEmpty()) {
            isTogo(user,userMapper);
        }
    }
}
